package com.pds.web.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.common.utils.SecurityUtils;
import com.pds.web.message.dto.MessageReadStatusDTO;
import com.pds.web.message.dto.MessageSendStatusDTO;
import com.pds.web.message.entity.PdsRemindMessage;
import com.pds.web.message.mapper.PdsRemindMessageMapper;
import com.pds.web.message.service.PdsRemindMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 部门机构通知信息表
 *
 * @author srf sunlightcs@gmail.com
 * @since 1.0.0 2024-02-05
 */
@Service("pdsRemindMessageService")
public class PdsRemindMessageServiceImpl extends ServiceImpl<PdsRemindMessageMapper, PdsRemindMessage> implements PdsRemindMessageService {
    @Resource
    private PdsRemindMessageMapper pdsRemindMessageMapper;

    @Override
    public List<PdsRemindMessage> selectList(PdsRemindMessage pdsRemindMessage) {
        QueryWrapper<PdsRemindMessage> wrapper = new QueryWrapper<>();
        Date messageDate = pdsRemindMessage.getMessageDate();
        String sendStatus = pdsRemindMessage.getSendStatus();
        String isRead = pdsRemindMessage.getIsRead();
        if(!Objects.isNull(messageDate)){
            wrapper.eq("message_date",messageDate);
        }
        if(!Objects.isNull(sendStatus) && !sendStatus.isEmpty()){
            wrapper.eq("send_status",sendStatus);
        }
        if(!Objects.isNull(isRead) && !isRead.isEmpty()){
            wrapper.eq("is_read",isRead);
        }
        return pdsRemindMessageMapper.selectList(wrapper);
    }

    @Override
    public PdsRemindMessage queryById(Long id) {
        return pdsRemindMessageMapper.selectById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public PdsRemindMessage insert(PdsRemindMessage pdsRemindMessage) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        /*Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        pdsRemindMessage.setUserId(userId);
        pdsRemindMessage.setCreateBy(nickName);*/
        pdsRemindMessage.setCreateTime(date);
        pdsRemindMessage.setSendStatus("1");
        pdsRemindMessage.setIsRead("1");
        pdsRemindMessageMapper.insert(pdsRemindMessage);
        return pdsRemindMessage;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean update(PdsRemindMessage pdsRemindMessage) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        /*String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        pdsRemindMessage.setUpdateBy(nickName);*/
        pdsRemindMessage.setUpdateTime(date);
        return pdsRemindMessageMapper.updateById(pdsRemindMessage) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        return pdsRemindMessageMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean updateSendStatus(MessageSendStatusDTO messageSendStatusDTO) {
        PdsRemindMessage pdsRemindMessage = new PdsRemindMessage();
        BeanUtils.copyProperties(messageSendStatusDTO,pdsRemindMessage);
        return pdsRemindMessageMapper.updateById(pdsRemindMessage) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean updateReadStatus(MessageReadStatusDTO messageReadStatusDTO) {
        PdsRemindMessage pdsRemindMessage = new PdsRemindMessage();
        BeanUtils.copyProperties(messageReadStatusDTO,pdsRemindMessage);
        return pdsRemindMessageMapper.updateById(pdsRemindMessage) > 0;
    }
}